Ext.define('ux.CustomDateField', {
    extend: 'Ext.Container',
    xtype: 'custom_date_field',
    requires: [
    	'Ext.Label'
    ],
	constructor: function (config) {
		this.callParent(arguments); 
		this.getComponent('field-value').setHtml(config.value);
		this.getComponent('field-value').setData(config.data);
		this.getComponent('field-value').setId(config.id);
		this.getComponent('field-value').addListener({
			tap:function () {
	        	var dateStr = Ext.getCmp(config.id).getComponent('field-value').getData();
	        	var resultCallback = function(result) {
	        		var returnDate = result.datetime;
	        		if(returnDate!=null && returnDate!=""){
	    				Ext.getCmp(config.id).getComponent('field-value').setHtml(returnDate);
	                    Ext.getCmp(config.id).getComponent('field-value').setData(returnDate);
	                    if(config.id == "salesProgressBeginDate" || config.id == "salesProgressEndDate"){
	                    	custom.salesProgressView(0);
	                    }else if(config.id == "salesProgressBeginMonth" || config.id == "salesProgressEndMonth"){
	                    	custom.salesProgressView(1);
	                    }else if(config.id == "callCardRecordBeginDate" || config.id == "callCardRecordEndDate"){
	                    	var beginDateStr = Ext.getCmp("callCardRecordBeginDate").getComponent('field-value').getData();
							var endDateStr = Ext.getCmp("callCardRecordEndDate").getComponent('field-value').getData();
	                    	var date1 = beginDateStr.split("-");
							var date2 = endDateStr.split("-");
							date1 = new Date(date1[0],date1[1],date1[2]);
							date2 = new Date(date2[0],date2[1],date2[2]);
							var days= date2.getTime()-date1.getTime();
							var diffDay = parseInt(days/(1000*60*60*24));
	                    	if(Number(Math.abs(diffDay)) >= 44){
	                    		if(config.id == "callCardRecordBeginDate"){
									var endDate = Date.parse(beginDateStr);
									endDate = endDate+(44*24*60*60*1000);
									endDate = new Date(endDate)
									var endDateStr = '';
			    					var month = parseInt(endDate.getMonth()+1);
			    				    endDateStr += endDate.getFullYear();
			    				    endDateStr += '-';
			    					endDateStr += month < 10 ? '0'+month : month;
			    					endDateStr += '-';
			    					endDateStr += endDate.getDate() < 10 ? '0'+endDate.getDate() : endDate.getDate();
	                    			Ext.getCmp("callCardRecordEndDate").getComponent('field-value').setHtml(endDateStr);
	                    			Ext.getCmp("callCardRecordEndDate").getComponent('field-value').setData(endDateStr);
	                    		}else{
	                    			var beginDate = Date.parse(endDateStr);
									beginDate = beginDate-(44*24*60*60*1000);
									beginDate = new Date(beginDate)
									var beginDateStr = '';
			    					var month = parseInt(beginDate.getMonth()+1);
			    				    beginDateStr += beginDate.getFullYear();
			    				    beginDateStr += '-';
			    					beginDateStr += month < 10 ? '0'+month : month;
			    					beginDateStr += '-';
			    					beginDateStr += beginDate.getDate() < 10 ? '0'+beginDate.getDate() : beginDate.getDate();
	                    			Ext.getCmp("callCardRecordBeginDate").getComponent('field-value').setHtml(beginDateStr);
	                    			Ext.getCmp("callCardRecordBeginDate").getComponent('field-value').setData(beginDateStr);
	                    		}
	                    	}
	                    	custom.callCardRecordView();
	                    }else if(config.id == "orderRecordBeginDate" || config.id == "orderRecordEndDate"){
	                    	if(Ext.getCmp('RadioDate1').getSrc().indexOf('radio_checked.png')>-1){
	                    		if(config.activate_tap == 'reportHome'){
	                    			custom.orderRecordReportView(1);
	                    		}else{
	                    			custom.orderRecordView(1);
	                    		}
	                    	}else if(Ext.getCmp('RadioDate2').getSrc().indexOf('radio_checked.png')>-1){
	                    		if(config.activate_tap == 'reportHome'){
	                    			custom.orderRecordReportView(2);
	                    		}else{
	                    			custom.orderRecordView(2);
	                    		}
	                    	}else if(Ext.getCmp('RadioDate3').getSrc().indexOf('radio_checked.png')>-1){
	                    		if(config.activate_tap == 'reportHome'){
	                    			custom.orderRecordReportView(3);
	                    		}else{
	                    			custom.orderRecordView(3);
	                    		}
	                    	}
	                    }else if(config.id == "callCardEndDate"){
	                    	custom.callCardView();
	                    }
	        		}
	        	};
	        	navigator.datetimepicker.datepicker(resultCallback, ['確定','取消'],dateStr);
    		}, 
    		element:'element'
    	});
	},
	
    config: {
		layout: {
			type: 'hbox'
		},
		style: 'margin-top:0.35rem;height:1.5rem;line-height:1.5rem;border: 1px solid #B9B9B9;border-radius:5px;background-image: url(./resources/images/select_date.png);background-repeat:no-repeat;background-position:center;background-size:0.5rem;background-position-x:95%;padding-left:0.2rem;',
		items: [{
			flex : 1,
			xtype: 'label',
			itemId: 'field-value',
			id: '',
			data: '',
			listeners: {
				element : 'element',
				tap : function () {
				
				}
			}
		}]
	}
});